package com.gome.ocean.common.utils;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author zhangguoqing 2016年12月29日 下午6:26:42
 * @since 5.0.0
 */
public class CompressFileUtils {

    private static final String DEFAULT_SUFFIX_NAME = ".sql";
    private static final Logger LOG                 = LoggerFactory.getLogger(CompressFileUtils.class);

    /**
     * 压缩字节文件成zip
     * 
     * @param file输出文件名
     * @param bt文件流
     * @param encoding字符编码
     * @param entryName文件实例名称
     * @param suffixName文件属性后缀
     */
    public static void compressByteFile(File file, byte[] bt, String encoding, String entryName,
                                        String suffixName) throws FileNotFoundException {
        if (StringUtils.isBlank(suffixName)) {
            suffixName = DEFAULT_SUFFIX_NAME;
        }
        CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream(file), new CRC32());
        ZipOutputStream outStream = null;
        try {
            outStream = new ZipOutputStream(cos, Charset.forName(encoding));
            outStream.putNextEntry(new ZipEntry(entryName.concat(suffixName)));
            IOUtils.copy(new ByteArrayInputStream(bt), outStream);
            outStream.closeEntry();
        } catch (IOException e) {
            e.printStackTrace();
            LOG.error("tar is error!", e);
        } finally {
            IOUtils.closeQuietly(outStream);
        }
    }

    /**
     * 批量压缩字节文件成zip
     */
    public static void compressBatchByteFile(File file, List<Map<String, Object>> entryMap, String encoding) {
        for (Map<String, Object> map : entryMap) {
            String key = map.get("fileKey").toString();
            String value = map.get("fileValue").toString();
            String suffixValue = map.get("suffixValue").toString();
            try {
                compressByteFile(file, value.getBytes(), encoding, key, suffixValue);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                LOG.error("batchDeal is error!", e);
            }
        }
    }

    /**
     * @description 字节转成文件file
     * @param filePath 文件存放路径
     * @param outFileName 输出文件名称
     * @param inputFileByte 输入流文件内容字节
     */
    public static File byteToFile(String filePath, String outFileName, byte[] inputFileByte) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            if (!dir.exists() && dir.isDirectory()) {
                dir.mkdirs();
            }
            file = new File(filePath + File.separator + outFileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(inputFileByte);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return file;
    }
}
